package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    @Override
    public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        // 判断加入购物车的商品是否存在  根据 套餐id 或 菜品 id
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        // 用户id
        shoppingCart.setUserId(BaseContext.getCurrentId());
        List<ShoppingCart> shoppingCartS = shoppingCartMapper.selectByUserId(shoppingCart);
        //判断
        if (shoppingCartS.size() > 0 && shoppingCartS != null) {
            // 已存在  update number + 1  只能查到一条数据
            ShoppingCart cart = shoppingCartS.get(0);
            cart.setNumber(cart.getNumber() + 1);
            // 更新number数据
            shoppingCartMapper.update(cart);

        } else {
            // 不存在  insert
            // 判断添加到购物车的 是套餐还是菜品
            // 分别获取套餐id和菜品id
            Long dishId = shoppingCartDTO.getDishId();
            Long setmealId = shoppingCartDTO.getSetmealId();
            if (dishId != null) {
                // 菜品
                Dish dish = dishMapper.selectById(dishId);
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setName(dish.getName());
                shoppingCart.setAmount(dish.getPrice());
            } else {
                // 套餐
                Setmeal setmeal = setmealMapper.selectById(setmealId);
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setAmount(setmeal.getPrice());
            }
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartMapper.insert(shoppingCart);
        }

    }

    /**
     * 查看购物车
     *
     * @param userId
     * @return
     */
    @Override
    public List<ShoppingCart> queryAllByUserId() {
        Long userId = BaseContext.getCurrentId();
        return shoppingCartMapper.selectAllByUserId(userId);
    }

    /**
     * 清空购物车
     */
    @Override
    public void deleteCarts() {
        Long userId = BaseContext.getCurrentId();
        shoppingCartMapper.delete(userId);
    }

    /**
     * 对购物车单删
     *
     * @param shoppingCartDTO
     * @param shoppingCartDTO1
     */
    @Override
    public void deleteOne(ShoppingCartDTO shoppingCartDTO) {
        // dto 转换 entity
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        // 获取用户id
        Long userId = BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);
        // 根据用户id 和  套餐id 或 菜品 id 进行查询操作 查看该商品的number是否为1
        List<ShoppingCart> shoppingCarts = shoppingCartMapper.selectByUserId(shoppingCart);
        // 只能查询出一个数据 将数据赋值给 shoppingCart
        shoppingCart = shoppingCarts.get(0);
        if (shoppingCart.getNumber() > 1) {
            // > 1  对数量进行修改  update - 1
            shoppingCart.setNumber(shoppingCart.getNumber() - 1);
            shoppingCartMapper.update(shoppingCart);
        } else {
            // 为1  直接删除
            shoppingCartMapper.deleteOne(shoppingCart);
        }

    }
}
